using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace ISpy
{
    /// <summary>
    /// This is the main type for your game
    /// This is the BIG PAPA of our class structure
    /// It is responsible for the loading everything
    /// Calls classes Renders and Updates based on the GameState
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        ///////////////////////////////////////////////////
        //  GAME CLASS DATA MEMBERS
        ///////////////////////////////////////////////////
        /// <summary>
        /// GameState enum to be used to differentiate between varying states
        /// Gamestarts with SPLASH screen
        /// </summary>
        public enum GameState { SPLASH = 0, MENU, INGAME, PAUSE };

        GameState currentState;
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D splashTexture;
        Rectangle clientRect;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            this.graphics.PreferredBackBufferWidth = 1280;
            this.graphics.PreferredBackBufferHeight = 720;
            this.graphics.IsFullScreen = true;

        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            currentState = GameState.SPLASH;

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            splashTexture = Content.Load<Texture2D>("003 copy");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            KeyboardState newState = Keyboard.GetState();
            if(newState.IsKeyDown(Keys.Escape))
                this.Exit();

            // TODO: Add your update logic here
            switch (currentState)
            {
                case GameState.SPLASH:
                    SplashUpdate(gameTime);
                    break;
                case GameState.MENU:
                    break;
                case GameState.INGAME:
                    break;
                case GameState.PAUSE:
                    break;
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// Displays splash screen on startup
        /// </summary>
        /// <param name="gameTime">GameTime elapsed, just pass it</param>
        private void SplashUpdate(GameTime gameTime)
        {
            clientRect = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
            if (gameTime.TotalRealTime >= new TimeSpan(0, 0, 5))
                ChangeState(GameState.MENU);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Tomato);

            // TODO: Add your drawing code here
            switch (currentState)
            {
                case GameState.SPLASH:
                    SplashRender(gameTime);
                    break;
                case GameState.MENU:
                    break;
                case GameState.INGAME:
                    break;
                case GameState.PAUSE:
                    break;
            }

            base.Draw(gameTime);
        }

        /// <summary>
        /// Renders the splash screen at startup
        /// </summary>
        /// <param name="gameTime">GameTime elapsed, just pass it</param>
        private void SplashRender(GameTime gameTime)
        {
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            spriteBatch.Draw(splashTexture, clientRect, Color.Navy);
            spriteBatch.End();
        }

        /// <summary>
        /// Changes the current state of the game, changing its Update and Render functions
        /// </summary>
        /// <param name="newState">The new state to which the current state should be changed</param>
        public void ChangeState(GameState newState)
        {
            currentState = newState;
        }
    }
}
